<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="spring" uri="/WEB-INF/tlds/spring.tld"%>
<%@ taglib prefix="c" uri="/WEB-INF/tlds/c.tld"%>
<c:set var="BasePath" value="${pageContext.request.contextPath}"></c:set>
<c:set var="Plugins" value="${pageContext.request.contextPath}/res/plugins"></c:set>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>关联采购单</title>
<link href="${Plugins}/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet" type="text/css" />
<link href="${Plugins}/ligerUI/skins/ligerui-icons.css" rel="stylesheet" type="text/css" />
<script src="${Plugins}/jquery/jquery-1.3.2.min.js"	type="text/javascript"></script>
<script src="${Plugins}/ligerUI/js/ligerui.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.validate.min.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/jquery.metadata.js" type="text/javascript"></script>
<script src="${Plugins}/jquery-validation/messages_cn.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.form.js" type="text/javascript"></script>
<script src="${BasePath}/res/js/common/jquery.validate.method.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/ligerCommon.js"></script>
<script src="${Plugins}/datepicker/WdatePicker.js" type="text/javascript"></script>
<script type="text/javascript" src="${BasePath}/res/js/json2.js"></script>

<style type="text/css">
    body{ font-size:12px;}
    .l-table-edit {}
    .l-table-edit-td{ padding:4px;}
    .l-button-submit,.l-button-test{width:80px; float:left; margin-left:10px; padding-bottom:2px;}
    .l-verify-tip{ left:230px; top:120px;}
    .navbar {
		height: 20px;
		border-bottom: #E6E6E6 solid 1px;
		line-height: 20px;
	}
	.p-tab .s-lab{font-size:12px;font-family:"微软雅黑",​"宋体",​Arial,​sans-serif;}
	input.l-textarea{width:170px;height:20px;font-size:12px;}
</style>
</head>
<body style="padding:10px">
<div class="navbar">
		<span>您当前的位置：</span>
		出入库管理  &gt;出库管理 &gt;关联采购单
</div>
<form name="form1" method="post" action="addStockOutPurInfo.do" id="form1">
   <table  cellpadding="0" cellspacing="0" class="l-table-edit p-tab">
    <tr>
        <td align="right" class="l-table-edit-td s-lab"></td>
        <td align="left" width="100%" class="l-table-edit-td "></td>
        <td align="left"></td>
    </tr>
     <tr>
        <td align="right" class="l-table-edit-td s-lab">
        	<input type="button" value="选择采购单" id="select_pur" class="l-button l-button-test" />
        </td>
        <td align="right"  class="l-table-edit-td s-lab">
            <input type="button" value="保存" id="add_pur" class="l-button l-button-test" onclick="addStockPur()"/> 
                       出库总数：<!-- <span id = "counter" style="color:blue">0/</span> -->${stockOutProduct.stockoutQuantity}
        </td>
        <td align="left" class="l-table-edit-td ">
        	<input type="hidden" id="productlist" name="productlist" />
        </td>
     </tr>
     <tr>
        <td colspan="3" align="left" class="l-table-edit-td s-lab">
        <div id="maingrid" style="margin:0; padding:0"></div>
		</td>
     </tr>
     <tr>
        <td colspan="3" align="left" class="l-table-edit-td s-lab">
		</td>
     </tr>
</table>
<div style="display:none;"></div>
</form>
</body>
<script>
	var basePath ="${BasePath}";
	var addStockPurUrl = '${BasePath}/stockmanage/StockOutPurInfo/addStockOutPurInfo.do';
	$(document).ready(function(){
	
		$("#select_pur").click(function(){
			list = new Array();
		   	var m =$.ligerDialog.open({
		   				title:'选择采购单商品',
		   				url:'${BasePath}/stockmanage/StockOutPurInfo/toQueruyStockPur.do?prodNo=${stockOutProduct.prodNo}&whNo=${stockOutProduct.whNo}',
		   				height:500,width:700,
		   				buttons: [{ text: '确定', onclick: function (item, dialog) { 
		   									dialog.frame.valProd();
		   									if(!dialog.frame.isOut) return false;
		   									addProduct(); 
		   									dialog.close(); 
		   								} 
		   						  },
		   				          { text: '取消', onclick: function (item, dialog) { dialog.close(); list = new Array();} } ],
		   				isResize: false, 
		   				isDrag: false 
		   		});
		   	m.show(); 
		});
		
		addProduct();
	});
	
	//临时请购商品集合
	var list = new Array();
	//请购商品集合
	var commoditylist = new Array();
	var g = null;
	//请购商品列表
	function addProduct(){
		commoditylist = commoditylist.concat(list);
		//$.ligerDialog.warn(JSON.stringify(commoditylist));
		commodityData = {"Rows": commoditylist, "Total": commoditylist.length};
		grid = $("#maingrid").ligerGrid({
	        columns: [
	      //{ display: 'id', name: 'stockPurId' },
	        { display: '货品编号', name: 'prodNo' },
	        { display: '商品编号', name: 'commoNo' },
	        { display: '商品名称', name: 'commoName' },
			{ display: '采购单号', name: 'poNo'},
			{ display: '供应商', name: 'supName' },
			{ display: '仓库名称', name: 'whName' },
	      	{ display: '规格', name: 'specName'},
	        { display: '库存良品数', name: 'yieldAmountNumner'},
	        { display: '需出库数', name: 'stockoutQuantity',editor:{type:'int'},
	             render:function(rowdata,rowindex,value){
					return value;
	     		}
	        },
	        { display: '操作', isSort: false, render: function (rowdata, rowindex, value){
	           	var  text = "<input type='button' value='删除' onclick='removeThis("+rowindex+");' />";
	            return text;
	        	}
	        }
	        ], 
	        width: '100%',
	        height: '330',
	        usePager: false,enabledEdit: true,
	        data: $.extend(true,{},commodityData)
	    });
		list = new Array();
		//
		  g= $("#maingrid").ligerGrid({
			         grid: grid,
		 			pageSize:30,
		            width: '100%',
		            height: '92%',
		            dataType: 'server', 
		            dataAction: 'server',
		            pagesizeParmName: 'pageSize',
		            enabledEdit: true,
					onBeforeSubmitEdit: onBeforeSubmitEdit
		        });
		//
	}
	 function vailGridData(){
		var maxCount = ${stockOutProduct.stockoutQuantity};
		if(g.getData().length == 0){
			window.parent.parent.f_tip('请选择采购单商品后再点击保存提交');
			return false;
		}
		var stockoutQuantityCount = getStockoutQuantityCount();
		if(stockoutQuantityCount != maxCount){
			window.parent.parent.f_tip('当前采购单商品出库数量['+stockoutQuantityCount+'],不等于总数量[' + maxCount + ']件。请调整数量后点击保存');
			return false;
		}
		return true;
	 }

	 function getSubmitData(){
		 var submitData = [];
		 var datas = g.getData(); 
		 for(var i = 0;i< datas.length;i++){
			 var data = {};
			 data.stockOutTypeNo = "${stockOut.stockOutTypeNo}";
			 data.stockOutType = "${stockOut.stockOutType}";
			 data.stockOutNo = "${stockOutProduct.stockOutNo}";
			 data.stockOutProductId = "${stockOutProduct.id}";
			 data.stockoutQuantity = datas[i].stockoutQuantity;
			 data.stockPurId = datas[i].stockPurId;
			 data.purNo =  datas[i].poNo;
			 data.prodNo =  datas[i].prodNo;
			 submitData[i] = data;
		 }
		 return submitData;
	 }
	 
	 function resetCounter(count){
		if(count == null){
			count = 0
		}
		$('#counter').html(getStockoutQuantityCount() + count);
	 }
	 
	 function getStockoutQuantityCount(){
		 var data = g.getData();
		 var count = 0;
		 for(var i = 0 ; i < data.length;i++){
			 count += data[i].stockoutQuantity;
		 }
		 return count;
	 }
	 
	 function addStockPur(){
		 if(vailGridData()){
		 	$.ajax({
				type : 'post',
				dataType : 'text',
				data : {
					stockOutPurInfo : JSON.stringify(getSubmitData())
				},
				url : addStockPurUrl,
				success : function(data) {
					if (data == 'fail') {
						window.parent.parent.f_tip('新增失败');
					} else {
						window.parent.parent.f_tip('新增成功');
						parent.window.updateCellForProductCodeState(${rowindex}); 
						window.parent.$.ligerDialog.close();
						window.parent.$(".l-dialog,.l-window-mask").remove();
						//window.parent.location.reload();
					}
				}
			});
		 }
	 }

	function onBeforeSubmitEdit(e){ 
		var record = e.record;
		var column = e.column;
		var editValue = e.value;
		if(column.name == "stockoutQuantity"){
			var maxQuantity = record.yieldAmountNumner;
			if(editValue < 1){
				e.value = 1;
				window.parent.parent.f_tip('出库商品至少为1件');
			}else if(editValue > maxQuantity){
				e.value = maxQuantity;
				window.parent.parent.f_tip('出库商品不能大于' + maxQuantity);
			}
		}
	}
	 
	//删除商品
	function removeThis(o) {
		commoditylist.splice(o,1);
		addProduct();
	}

	//from提交
	$(function ()
			{
			    $.metadata.setType("attr", "validate");
			    var v = $("form").validate({
			        errorPlacement: function (lable, element)
			        {
			        	lable.ligerHideTip();
			            if (element.hasClass("l-textarea"))
			            {
			                element.ligerTip({ content: lable.html(), target: element[0] }); 
			            }
			            else if (element.hasClass("l-text-field"))
			            {
			                element.parent().ligerTip({ content: lable.html(), target: element[0] });
			            }
			            else
			            {
			                lable.appendTo(element.parents("td:first").next("td"));
			            }
			            
			        },
			        submitHandler:function()
			        {
			        	$("form .l-text,.l-textarea").ligerHideTip();
			            var options = {
			            		type: 'POST',
			            		beforeSend: function(){
			            			$("#form1").find(":submit,:button").attr("disabled", true);
			            		},
			            		success: function(data){
			            			if(data=="success") {
			            				window.parent.f_tip('新增成功');
			            				close_liger_tab();
			            			} else {
			            				window.parent.f_tip('新增失败');
			            			}
			            		}
			            	}, $form1 = $("#form1");
			           $form1.ajaxSubmit(options);
			           return false;
			        }
			    });
	}); 
</script>
</html>